home *** CD-ROM | disk | FTP | other *** search
/ Flash MX Savvy / FlashMX Savvy.iso / pc / MAC / Amapi3D / Amapi3DTrial_Edition / 3SPACE / TSMath.js < prev    next >
Encoding:
Text File  |  2001-02-20  |  4.3 KB  |  240 lines  |  [AMAS/AMAP]

  1. // -* TSMath.js *-
  2. //
  3. // Description: Basic maths
  4. // Author:
  5. // Version: $Id: TSMath.js,v 1.5 2000/12/08 18:07:31 consumer Exp $
  6. //
  7.  
  8. //
  9. // Conversions
  10. //
  11.  
  12. function degToRad(angleDeg)
  13. {
  14.   return angleDeg * Math.PI / 180;
  15. }
  16.  
  17. function radToDeg(angleRad)
  18. {
  19.   return angleRad / Math.PI * 180;
  20. }
  21.  
  22. //
  23. // Point
  24. //
  25.  
  26. function Point(x, y, z)
  27. {
  28.   this.x = x;
  29.   this.y = y;
  30.   this.z = z;
  31. }
  32.  
  33. function makePoint(x, y, z)
  34. {
  35.   return new Point(x, y, z);
  36. }
  37.  
  38. function makePointFromString(str)
  39. {
  40.   // Resolves bug on NS
  41.   str = str + '\0';
  42.   var strSplit = str.split(" ");
  43.   return new Point(strSplit[0], strSplit[1], strSplit[2]);
  44. }
  45.  
  46. function makeStringFromPoint(pt)
  47. {
  48.   return (pt.x + ' ' + pt.y + ' ' + pt.z);
  49. }
  50.  
  51. //
  52. // Point operations
  53. //
  54.  
  55. function pointRotateX(pt, angle)
  56. {
  57.   new_x = pt.x
  58.   new_y = (pt.y * Math.cos(angle)) + (pt.z * Math.sin(angle))
  59.   new_z = (pt.z * Math.cos(angle)) - (pt.y * Math.sin(angle))
  60.  
  61.   return new Point(new_x, new_y, new_z)
  62. }
  63.  
  64. function pointRotateY(pt, angle)
  65. {
  66.   new_x = (pt.x * Math.cos(angle)) - (pt.z * Math.sin(angle))
  67.   new_y = pt.y
  68.   new_z = (pt.x * Math.sin(angle)) + (pt.z * Math.cos(angle))
  69.  
  70.   return new Point(new_x, new_y, new_z)
  71. }
  72.  
  73. function pointRotateZ(pt, angle)
  74. {
  75.   new_x = (pt.x * Math.cos(angle)) + (pt.y * Math.sin(angle))
  76.   new_y = (pt.y * Math.cos(angle)) - (pt.x * Math.sin(angle))
  77.   new_z = pt.z
  78.  
  79.   return new Point(new_x, new_y, new_z)
  80. }
  81.  
  82. function pointSphericalRotate(theta, phi)
  83. {
  84.   fphi = parseFloat(phi);
  85.   ftheta = parseFloat(theta);
  86.  
  87.   new_x = Math.cos(fphi) * Math.sin(ftheta);
  88.   new_y = Math.sin(fphi);
  89.   new_z = Math.cos(fphi) * Math.cos(ftheta);
  90.   
  91.   return makePoint(new_x, new_y, new_z);
  92. }
  93.  
  94. function pointNegate(pt)
  95. {
  96.   return makePoint(-parseFloat(pt.x),
  97.                    -parseFloat(pt.y),
  98.                    -parseFloat(pt.z));
  99. }
  100.  
  101. function pointTranslate(pt, x, y, z)
  102. {
  103.   return makePoint(parseFloat(pt.x) + parseFloat(x), 
  104.                    parseFloat(pt.y) + parseFloat(y), 
  105.                    parseFloat(pt.z) + parseFloat(z));
  106. }
  107.  
  108. function pointScale(pt, x, y, z) 
  109. {
  110.   return makePoint(parseFloat(pt.x) * parseFloat(x), 
  111.                    parseFloat(pt.y) * parseFloat(y), 
  112.                    parseFloat(pt.z) * parseFloat(z));
  113. }
  114.  
  115. function pointMultiply(pt, c)
  116. {
  117.   return makePoint(parseFloat(pt.x) * parseFloat(c), 
  118.                    parseFloat(pt.y) * parseFloat(c), 
  119.                    parseFloat(pt.z) * parseFloat(c));
  120. }
  121.  
  122. function pointDistance(pt1, pt2) 
  123. {
  124.   return vectorLength(makeVector(pt1, pt2));
  125. }
  126.  
  127. function pointInterpolate(pt1, pt2, alpha)
  128. {
  129.   return makePoint(parseFloat(pt1.x) * (1.0 - alpha) + parseFloat(pt2.x) * alpha,
  130.                    parseFloat(pt1.y) * (1.0 - alpha) + parseFloat(pt2.y) * alpha,
  131.                    parseFloat(pt1.z) * (1.0 - alpha) + parseFloat(pt2.z) * alpha);
  132. }
  133.  
  134. //
  135. // Vector operations
  136. // (The vector structure is a point)
  137. //
  138.  
  139. function makeVector(pt1, pt2)
  140. {
  141.   return makePoint(pt2.x - pt1.x, pt2.y - pt1.y, pt2.z - pt1.z);
  142. }
  143.  
  144. function vectorLength(v)
  145. {
  146.   return Math.sqrt((v.x * v.x) + (v.y * v.y) + (v.z * v.z));
  147. }
  148.  
  149. function vectorNormalize(v)
  150. {
  151.   return pointMultiply(v, 1 / vectorLength(v));
  152. }
  153.  
  154. //
  155. // Bounding box
  156. //
  157.  
  158. function BoundingBox(pointMin, pointMax)
  159. {
  160.   this.min = pointMin;
  161.   this.max = pointMax;
  162. }
  163.  
  164. function makeBoundingBox(pointMin, pointMax)
  165. {
  166.   var bbox = new BoundingBox(pointMin, pointMax);
  167.  
  168.   // [HACK] Le constructeur n'a pas l'air de fonctionner!! :-(
  169.   bbox.min = pointMin;
  170.   bbox.max = pointMax;
  171.  
  172.   return bbox;
  173. }
  174.  
  175. function makeBoundingBoxFromString(str)
  176. {
  177.   // Resolves bug on NS
  178.   str = str + '\0';
  179.   var strSplit = str.split(" ");
  180.   var min = new Point(strSplit[0], strSplit[1], strSplit[2]);
  181.   var max = new Point(strSplit[3], strSplit[4], strSplit[5]);
  182.  
  183.   return makeBoundingBox(min, max);
  184. }
  185.  
  186. function boundingBoxGetLengthX(bbox)
  187. {
  188.   return (bbox.max.x - bbox.min.x);
  189. }
  190.  
  191. function boundingBoxGetLengthY(bbox)
  192. {
  193.   return (bbox.max.y - bbox.min.y);
  194. }
  195.  
  196. function boundingBoxGetLengthZ(bbox)
  197. {
  198.   return (bbox.max.z - bbox.min.z);
  199. }
  200.  
  201. //
  202. // Color
  203. //
  204.  
  205. function Color(r, g, b)
  206. {
  207.   this.r = r;
  208.   this.g = g;
  209.   this.b = b;
  210. }
  211.  
  212. function makeColor(r, g, b)
  213. {
  214.   return new Color(r, g, b);
  215. }
  216.  
  217. function makeColorFromString(str)
  218. {
  219.   // Resolves bug on NS
  220.   str = str + '\0';
  221.   strSplit = str.split(" ");
  222.   return new Color(strSplit[0], strSplit[1], strSplit[2]);
  223. }
  224.  
  225. function makeStringFromColor(col)
  226. {
  227.   return (col.r + ' ' + col.g + ' ' + col.b);
  228. }
  229.  
  230. //
  231. // Color operations
  232. //
  233.  
  234. function colorMultiply(col, c)
  235. {
  236.   return makeColor(parseFloat(col.r) * c, 
  237.                    parseFloat(col.g) * c, 
  238.                    parseFloat(col.b) * c);
  239. }
  240.